package tuding.android.bigplanettracks.tracks;

import tuding.android.bigplanettracks.R;
import tuding.android.bigplanettracks.maps.storage.SQLLocalStorage;
import tuding.android.bigplanettracks.maps.tuding.ActionNotes;
import tuding.android.bigplanettracks.maps.tuding.InfoWindow;
import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

	private Context mContext;
	private Bitmap[] mThumbIds;

	public ImageAdapter(Context c, long trackid) {
		mContext = c;
		ActionNotes d = new ActionNotes();
		Cursor cursor = d.getActionPhotoCursorByTrackID(trackid);
		if(cursor != null) {
			Log.d("SHARE","photo num is "+cursor.getCount());
			mThumbIds = new Bitmap[cursor.getCount()];
			for (int i=0; i < cursor.getCount(); i++) {
				String pfname = cursor.getString(cursor.getColumnIndexOrThrow("uri"));
				Bitmap smallphoto = InfoWindow.getBitmapFromLocalPath(SQLLocalStorage.TRACK_PATH+"photo/thumbnail/thumbnail"+pfname, 1);
				Log.i("SHARE", "smallphoto");
				mThumbIds[i] = smallphoto;
				cursor.moveToNext();
			}
		}
		Log.d("SHARE", "ImageAdapter, mThumbIds length is "+mThumbIds.length);
		cursor.close();
		Log.d("SHARE", "ImageAdapter, 2mThumbIds length is "+mThumbIds.length);
	}

	@Override
	public int getCount() {
		Log.d("SHARE", "ImageAdapter, getCount length is "+mThumbIds.length);
		return mThumbIds.length; 
	}

	@Override
	public Object getItem(int position) {
		Log.d("SHARE", "ImageAdapter, getItem position is "+position);
		return mThumbIds[position];
	}

	@Override
	public long getItemId(int position) {
		Log.d("SHARE", "ImageAdapter, getItem position is "+position);
		return position;
	}



	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageview;
		if (convertView == null) {
			imageview = new ImageView(mContext);
			imageview.setLayoutParams(new GridView.LayoutParams(120, 120));
			imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageview.setPadding(8, 8, 8, 8);
		} else {
			imageview = (ImageView) convertView;
		}
		Log.d("SHARE", "position is "+position);
		imageview.setImageBitmap(mThumbIds[position]);
		return imageview;
	}
	

}
